package texts;


import moment.Utiles;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

import static moment.Utiles.driver;

public class LoginPage extends Utiles {
    public static String url = "http://110.41.84.205:9091/sign-in.html";
    public LoginPage() {
        super(url);
    }
    /**
     * 打开注册页面
     */
    public void LoginPageRight() {
        String text = driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2")).getText();
        assert "用户登录".equals(text);
        driver.findElement(By.cssSelector("#submit"));
    }

    public void LoginPageFail(String userName, String password) throws InterruptedException {
        // 建立显示等待,等待3,秒
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));

        /**
         * 校验密码错误
         */
        driver.findElement(By.cssSelector("#username")).sendKeys(userName);
        driver.findElement(By.cssSelector("#password")).sendKeys(userName);
        driver.findElement(By.cssSelector("#submit")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2"),"警告"));
        String warn = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert "警告".equals(warn);

        // 校验用户名错误
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#username")).sendKeys(password);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        wait.until(ExpectedConditions.textToBe(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2"),"警告"));
        warn = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert "警告".equals(warn);
    }

    public void LoginPageSec(String userName, String password) {
        LoginPageRight();
        // 清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        // 输入账号密码
        driver.findElement(By.cssSelector("#username")).sendKeys(userName);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#submit")).click();

        // 校验是否成功
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1)"));
    }
}
